package com.bqtt.tumor.demo.presentation.controller;

import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;

import com.bqtt.tumor.demo.business.LabSheetService;
import com.bqtt.tumor.demo.entity.LabSheet;
import com.bqtt.tumor.demo.entity.LabSheet;
import com.bqtt.tumor.demo.entity.SystemUser;

public class lab_sheet {

	public static String queryLabSheet(SystemUser user, ModelMap model,
			LabSheet condtion, BindingResult bindingResult) {
		// TODO Auto-generated method stub

		if(user.getSessionState() ==0)
		{// 用户未登入
			if(!model.containsAttribute("user"))
				model.addAttribute("user", new SystemUser());
			return "login";
		}
		
		if(condtion==null)
		{
			condtion = new LabSheet();
		}
		
		if(!model.containsAttribute("new_lab_sheet"))
		     model.addAttribute("new_lab_sheet", new LabSheet());		
		if(!model.containsAttribute("edit_lab_sheet"))
		     model.addAttribute("edit_lab_sheet", new LabSheet());	

		
		model.addAttribute("historySearchCondition", condtion);
		
		model.addAttribute("condition", condtion); //保留上次查找的字段
		
	    model.addAttribute("lab_sheet_list", LabSheetService.searchByCondition(condtion, 20, 1));
		
	    model.addAttribute("isAddNewShow","false");
		
		return "admin/lab_sheet";
	}

	public static String addLabSheet(SystemUser user, ModelMap model,
			LabSheet new_instance, BindingResult bindingResult) {
		// TODO Auto-generated method stub
				//session 变量 ——不用自动添加到Model
						//condition currentUser
						
						LabSheet historySearchCondition = (LabSheet) model.get("historySearchCondition");
						if(historySearchCondition==null)
							historySearchCondition = new LabSheet();
						
						if(!model.containsAttribute("new_lab_sheet"))
						     model.addAttribute("new_lab_sheet", new LabSheet());		
						if(!model.containsAttribute("edit_lab_sheet"))
						     model.addAttribute("edit_lab_sheet", new LabSheet());
						
						if(bindingResult!=null && bindingResult.hasErrors())
						{
							//出错的话，就保持弹出窗口，显示错误
							
							model.addAttribute("isAddNewShow","true");
							// 其他变量需要手动添加到Model
							model.addAttribute("lab_sheet_list", LabSheetService.searchByCondition(historySearchCondition, 20, 1));
							
							model.addAttribute("condition", historySearchCondition);
							
							return "admin/lab_sheet";
						}
						
						if(user.getSessionState() ==0)
						{// 用户未登入
							if(!model.containsAttribute("currentUser"))
							{
								model.addAttribute("currentUser", new SystemUser());
							}
							return "login";
						}
						

						boolean result = LabSheetService.add(new_instance);
						
						if(result)
						{
							model.addAttribute("new_lab_sheet", new LabSheet());		
							model.addAttribute("information", "<font style='color:#5599FF'>操作成功！</font>");
						}
						else
						{
							model.addAttribute("information", "<font style='color:#FF3333'>操作失败！</font>");
						}
						
						// 其他变量需要手动添加到Model
						model.addAttribute("condition", historySearchCondition);
						model.addAttribute("lab_sheet_list", LabSheetService.searchByCondition(historySearchCondition, 20, 1));
					    model.addAttribute("isAddNewShow","false");

						return "admin/lab_sheet";
	}

	public static String editLabSheet(SystemUser user, ModelMap model,
			LabSheet edit_instance, BindingResult bindingResult) {
		LabSheet historySearchCondition = (LabSheet) model.get("historySearchCondition");
		if(historySearchCondition==null)
			historySearchCondition = new LabSheet();
		
		if(!model.containsAttribute("new_lab_sheet"))
		     model.addAttribute("new_lab_sheet", new LabSheet());		
		if(!model.containsAttribute("edit_lab_sheet"))
		     model.addAttribute("edit_lab_sheet", new LabSheet());
		
		if(bindingResult!=null && bindingResult.hasErrors())
		{
			//出错的话，就保持弹出窗口，显示错误
			
			model.addAttribute("isAddNewShow","true");
			// 其他变量需要手动添加到Model
			model.addAttribute("lab_sheet_list", LabSheetService.searchByCondition(historySearchCondition, 20, 1,edit_instance.getSheet_def_Id()));
			
			model.addAttribute("condition", historySearchCondition);
			
			return "admin/lab_sheet";
		}
		
		if(user.getSessionState() ==0)
		{// 用户未登入
			if(!model.containsAttribute("currentUser"))
			{
				model.addAttribute("currentUser", new SystemUser());
			}
			return "login";
		}
		

		boolean result = LabSheetService.update(edit_instance);
		
		if(result)
		{
			model.addAttribute("new_lab_sheet", new LabSheet());		
			model.addAttribute("information", "<font style='color:#5599FF'>操作成功！</font>");
		}
		else
		{
			model.addAttribute("information", "<font style='color:#FF3333'>操作失败！</font>");
		}
		
		// 其他变量需要手动添加到Model
		model.addAttribute("condition", historySearchCondition);
		model.addAttribute("lab_sheet_list", LabSheetService.searchByCondition(historySearchCondition, 20, 1));
	    model.addAttribute("isAddNewShow","false");

		return "admin/lab_sheet";
	}

}
